package com.sky.te;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j

public class mySchedule {
    @Resource
    OrderMapper orderMapper;
//    @Scheduled(cron= "0/5 * * * * ?")
//    public  void executeTask(){
//        log.info("现在的时间是：{}", LocalDateTime.now());
//    }
    @Scheduled(cron = "0 0/15 * * * ? ")
    public void CancelTimeoutOrder(){
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime localDateTime1 = localDateTime.minusMinutes(15);
        List<Orders> list = orderMapper.getTimeOutOrder(1,localDateTime1);
        if(list!=null && !list.isEmpty()){
            for (Orders orders:list){
                Orders orders1 = new Orders();
                orders1.setId(orders.getId());
                orders1.setStatus(Orders.CANCELLED);
                orders1.setCancelReason("订单超时未支付");
                orderMapper.update(orders1);
            }
        }
        log.info("完成一次取消超时未支付订单");
    }

    @Scheduled(cron = "0 0 0 * * ? ")
    public void handelUncompleted(){
        List<Orders> orderByStatus = orderMapper.getOrderByStatus(Orders.DELIVERY_IN_PROGRESS);
        for(Orders order:orderByStatus){
            order.setStatus(Orders.COMPLETED);
            orderMapper.update(order);
        }
        log.info("处理未完成订单");
    }
}
